package com.oa.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.oa.entity.Forum;
import com.oa.entity.Reply;
import com.oa.entity.Topic;
import com.oa.entity.User;
import com.oa.service.ForumService;
import com.oa.service.ReplyService;
import com.oa.service.TopicService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller("topicAction")
@Scope("prototype")
public class TopicAction extends ActionSupport implements ModelDriven<Topic>,
		ServletRequestAware {

	private static final long serialVersionUID = 1L;
	@Resource
	private TopicService topicService;
	@Resource
	private ReplyService replyService;
	@Resource
	private ForumService forumService;

	private String showTopicUrl;
	private String forumId;

	private HttpServletRequest req;

	public String getForumId() {
		return forumId;
	}

	public void setForumId(String forumId) {
		this.forumId = forumId;
	}

	@Override
	public void setServletRequest(HttpServletRequest req) {
		this.req = req;

	}

	private Topic topic;

	public String getShowTopicUrl() {
		return showTopicUrl;
	}

	public void setShowTopicUrl(String showTopicUrl) {
		this.showTopicUrl = showTopicUrl;
	}

	public Topic getTopic() {
		return topic;
	}

	public void setTopic(Topic topic) {
		this.topic = topic;
	}

	@Override
	public Topic getModel() {
		if (topic == null)
			topic = new Topic();
		return topic;
	}

	// 显示完整的主题信息
	public String queryDetailTopic() {

		List<Reply> replyList = replyService.selectDetailTopic(topic.getPostingId());
		
		Topic topicNext = topicService.selectTopic(topic.getPostingId());
		
		String queryDetailTopicUrl = req.getServletPath() + "?postingId="+ topic.getPostingId();
		
		ActionContext.getContext().getSession().put("queryDetailTopicUrl", queryDetailTopicUrl);
		
		ActionContext.getContext().put("topicNext", topicNext);
		
		System.out.println(topicNext.getPostingContent()+"======");
		
		ActionContext.getContext().put("replyList", replyList);
		return "queryDetailTopic";
	}

	// 添加主题
	public String addTopic() {
		System.out.println("敢问进来了嘛？");
		ActionContext act = ActionContext.getContext();
		
		User postingAuthor = (User) act.getSession().get("user1");
		System.out.println(postingAuthor+"=====");
		Forum forum = (Forum) act.getSession().get("forumNext");
		showTopicUrl = act.getSession().get("showTopicUrl").toString();
		System.out.println(act+showTopicUrl+"=====");
		topic.setPostingAuthor(postingAuthor);
		topic.setForum(forum);
		topic.setPostingTitle(topic.getPostingTitle().trim());
		System.out.println(topic.toString());
		topicService.addTopic(topic);
		return "addTopic";
	}

	// 修改主题类型
	public String updateTopicType() {
		Topic topicNext = topicService.selectTopic(topic.getPostingId());
		topicNext.setTopicType(topic.getTopicType());
		topicService.updateTopic(topicNext);
		showTopicUrl = ActionContext.getContext().getSession()
				.get("showTopicUrl").toString();
		return "updateTopicType";
	}

	// 删除主题
	public String deleteTopic() {
		Topic topicNext = topicService.selectTopic(topic.getPostingId());
		
		topicNext.setForum(null);
		
		topicService.deleteTopic(topicNext);
		
		Topic lastTopic = topicService.selectLastTopic(forumId);
		
		Forum forum = forumService.seleteForum(forumId);
		
		forum.setLastTopic(lastTopic);
		
		forumService.updateForum(forum);
		
		showTopicUrl = ActionContext.getContext().getSession().get("showTopicUrl").toString();
		
		return "deleteTopic";
	}

	// 移动主题到其他版块
	public String updateTopicForum() {
		Forum forumNew = forumService.seleteForum(forumId);
		Topic topicNext = topicService.selectTopicName(topic.getPostingTitle());
		Forum forumFirst=forumService.seleteForum(topicNext.getForum().getForumId());
		//更新原来的版块
		forumFirst.setArticleCount(forumFirst.getArticleCount()-topic.getReplyCount()-1);
		forumFirst.setTopicCount(forumFirst.getTopicCount()-1);
		if(topicService.selectLastTopic(forumFirst.getForumId()).getPostingTitle().equals(topicNext.getPostingTitle()))
			forumFirst.setLastTopic(null);
		forumService.updateForum(forumFirst);
		
		topicNext.setForum(forumNew);
		topicService.updateTopic(topicNext);
		//更新主题新添加的版块
		forumNew.setArticleCount(forumNew.getArticleCount()+topic.getReplyCount()+1);
		forumNew.setTopicCount(forumNew.getTopicCount()+1);
		if(topicService.selectLastTopic(forumNew.getForumId()).getPostingTitle().equals(topicNext.getPostingTitle()))
			forumNew.setLastTopic(topicNext);
		forumService.updateForum(forumNew);
		
		Topic topicFirst=topicService.selectLastTopic(forumFirst.getForumId());
		forumFirst.setLastTopic(topicFirst);
		forumService.updateForum(forumFirst);
		
		showTopicUrl = ActionContext.getContext().getSession()
				.get("showTopicUrl").toString();
		return "updateTopicForum";
	}
}
